import '../utils/tools.dart';
import 'comparable.dart';
import 'serializable.dart';

class OrderModel extends Serializable implements Equable<int> {
  int orderId = 0;
  int memberId = 0;
  String orderNo = '';

  double payAmount = 0.0;
  double payedAmount = 0.0;
  double productAmount = 0.0;
  double costAmount = 0.0;
  double discountAmount = 0.0;
  double commissionAmount = 0.0;

  String payType = '';
  int payTime = 0;

  Map<String, double>? commissionSpecial;

  String platform = '';
  String appId = '';
  String formId = '';

  int levelId = 0;

  int createTime = 0;
  int deliverTime = 0;
  int confirmTime = 0;
  int commentTime = 0;
  int cancelTime = 0;
  int refundTime = 0;
  int deleteTime = 0;

  int rebated = 0;
  int rebateTime = 0;
  double rebateTotal = 0;

  int type = 0;
  int status = 0;
  int refundStatus = 0;
  String reason = '';

  String remark = '';

  int addressId = 0;
  String receiveName = '';
  String mobile = '';
  String province = '';
  String city = '';
  String area = '';
  String address = '';

  int postageAreaId = 0;
  double postage = 0;
  int invoiceId = 0;
  String expressNo = '';
  String expressCode = '';

  final String _type;

  List<OrderProductModel> products = [];

  OrderModel({
    required this.memberId,
    this.remark = '',
    this.addressId = 0,
    this.invoiceId = 0,
  }) : _type = 'new';
  OrderModel.fromJson(Map? model) : _type = '' {
    if (model == null) return;
    orderId = Tools.parseInt(model['order_id']);
    memberId = Tools.parseInt(model['member_id']);
    orderNo = model['order_no'] ?? '';

    payAmount = Tools.parseNumber(model['payamount']);
    payedAmount = Tools.parseNumber(model['payedamount']);
    productAmount = Tools.parseNumber(model['product_amount']);
    costAmount = Tools.parseNumber(model['cost_amount']);
    discountAmount = Tools.parseNumber(model['discount_amount']);
    commissionAmount = Tools.parseNumber(model['commission_amount']);

    if (model['commission_special'] is Map) {
      commissionSpecial = {};
      for (MapEntry entry in model['commission_special'].entries) {
        commissionSpecial![entry.key.toString()] =
            Tools.parseNumber(entry.value);
      }
    }

    payType = model['pay_type'] ?? '';
    payTime = Tools.parseInt(model['pay_time']);

    platform = model['platform'] ?? '';
    appId = model['appid'] ?? '';
    formId = model['form_id'] ?? '';

    levelId = Tools.parseInt(model['level_id']);

    createTime = Tools.parseInt(model['create_time']);
    deliverTime = Tools.parseInt(model['deliver_time']);
    confirmTime = Tools.parseInt(model['confirm_time']);
    commentTime = Tools.parseInt(model['comment_time']);
    cancelTime = Tools.parseInt(model['cancel_time']);
    refundTime = Tools.parseInt(model['refund_time']);
    deleteTime = Tools.parseInt(model['delete_time']);

    rebated = Tools.parseInt(model['rebated']);
    rebateTime = Tools.parseInt(model['rebate_time']);
    rebateTotal = Tools.parseNumber(model['rebate_total']);

    type = Tools.parseInt(model['type']);
    status = Tools.parseInt(model['status']);
    refundStatus = Tools.parseInt(model['refund_status']);
    reason = model['reason'] ?? '';

    remark = model['remark'] ?? '';

    addressId = Tools.parseInt(model['address_id']);
    receiveName = model['receive_name'] ?? '';
    mobile = model['mobile'] ?? '';
    province = model['province'] ?? '';
    city = model['city'] ?? '';
    area = model['area'] ?? '';
    address = model['address'] ?? '';

    postageAreaId = Tools.parseInt(model['postage_area_id']);
    postage = Tools.parseNumber(model['postage']);
    invoiceId = Tools.parseInt(model['invoice_id']);
    expressNo = model['express_no'] ?? '';
    expressCode = model['express_code'] ?? '';

    if (model['products'] is List) {
      products = model['products']
          .map<OrderProductModel>((item) => OrderProductModel.fromJson(item));
    }
  }
  @override
  int get compareField => orderId;

  @override
  Map toJson() {
    if (_type == 'new') {
      return {
        'member_id': memberId,
        'address_id': addressId,
        'invoice_id': invoiceId,
        'remark': remark,
        'products': products,
      };
    }
    return {
      'order_id': orderId,
      'order_no': orderNo,
      'member_id': memberId,
      'address_id': addressId,
      'invoice_id': invoiceId,
      'remark': remark,
      'products': products,
    };
  }
}

class OrderProductModel extends Serializable implements Equable<int> {
  int id = 0;
  int orderId = 0;
  int memberId = 0;
  int productId = 0;
  int skuId = 0;
  Map<String, String> skuSpecs = {};
  String productTitle = '';
  String productImage = '';
  double productOrigPrice = 0;
  double productPrice = 0;
  double productCostPrice = 0;
  double productWeight = 0;
  int count = 0;
  int sort = 0;

  final String _type;

  OrderProductModel({
    required this.skuId,
    required this.productId,
    required this.count,
  }) : _type = 'new';

  OrderProductModel.fromJson(Map? model) : _type = '' {
    if (model == null) return;
    id = Tools.parseInt(model['id']);
    orderId = Tools.parseInt(model['order_id']);
    memberId = Tools.parseInt(model['member_id']);
    productId = Tools.parseInt(model['product_id']);
    skuId = Tools.parseInt(model['sku_id']);
    productTitle = model['product_title'] ?? '';
    productImage = model['product_image'] ?? '';
    productOrigPrice = Tools.parseNumber(model['product_orig_price']);
    productPrice = Tools.parseNumber(model['product_price']);
    productCostPrice = Tools.parseNumber(model['product_cost_price']);
    productWeight = Tools.parseNumber(model['product_weight']);
    count = Tools.parseInt(model['count']);
    sort = Tools.parseInt(model['sort']);

    skuSpecs = {};
    if (model['sku_specs'] is Map) {
      for (MapEntry entry in model['sku_specs'].entries) {
        skuSpecs[entry.key.toString()] = entry.value.toString();
      }
    }
  }

  @override
  int get compareField => id;

  @override
  Map toJson() {
    if (_type == 'new') {
      return {
        'product_id': productId,
        'sku_id': skuId,
        'count': count,
      };
    }
    return {
      'id': id,
      'order_id': orderId,
      'member_id': memberId,
      'product_id': productId,
      'sku_id': skuId,
      'sku_specs': skuSpecs,
      'product_title': productTitle,
      'product_image': productImage,
      'product_orig_price': productOrigPrice,
      'product_price': productPrice,
      'product_cost_price': productCostPrice,
      'product_weight': productWeight,
      'count': count,
      'sort': sort,
    };
  }
}
